<extend name="Index/index" />

<block name="main">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">权限管理</h1>
    </div>


    <form class="form-inline" action="<{:U('Admin/rule_add')}>" method="post" onsubmit="return check()">
        <div class="form-group">
            父级：
            <!-- <label class="sr-only" for="exampleInputEmail2">Email address</label> -->
            <select name="pid" class="form-control">
                <option value="0">--------- 顶级 ---------</option>
                <foreach name="data" item="vo">
                    <option value="<{$vo.id}>"><?=str_repeat('&nbsp',$vo['count']*6).'┠┄'. '&nbsp'.$vo['title']?></option>
                </foreach>
            </select>
        </div>
        <div class="form-group">
            名称：
            <!-- <label class="sr-only" for="exampleInputEmail2">Email address</label> -->
            <input type="text" name="title" id="title" class="form-control" />
        </div>
        <div class="form-group">
            控制器/方法：
            <input type="text" name="name" id="name" class="form-control" />
        </div>
            <button type="submit" id="submit" class="form-control">添加新权限</button>
        
            <div style="margin:10px">（ 为防止误操作，请在数据库执行 [ 删除 ] 操作 ）</div>
    </form>
    
    <div class="panel panel-default">
            <div class="table-responsive">
                <table class="table table-striped b-t b-light">
                    <thead>
                        <tr>
                            <th  width="4%"><div align='center'>ID</div></th>
                            <th  width="8%"><div align='center'>名称</div></th>
                            <th  width="8%"><div align='center'>控制器 / 方法</div></th>
                            <th  width="8%"><div align='center'>备注</div></th>
                            <th  width="12%"><div align='center'>创建时间</div></th>
                            <th  width="10%"><div align='center'>操作</div></th>
                        </tr>
                    </thead>
                    <tbody>
                        <foreach name="data" item="vo" key='k'>
                            <tr>
                                <td align='center'><{$vo.id}></td>
                                <td title="<{$vo.title}>"><?=str_repeat('&nbsp',$vo['count']*6).'┠┄'. '&nbsp'.$vo['title']?></td>
                                <td align='center'><{$vo.name}></td>
                                <td align='center'><{$vo.remark}></td>
                                <td align='center'><{$vo.create_time|date="Y-m-d H:i:s",###}></td>
                                <td align='center'>
                                <if condition="in_array('Admin/admin_edit',$access)">
                                <a href="#" class="disabled">编辑</a>
                                <else/>
                                <a href="#"  data-toggle="modal" data-target="#myModal">编辑</a>
                                </if>
                                </td>
                            </tr>
                        </foreach>
                    </tbody>
                </table>
            </div>
        </div>

                <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">编辑权限</h4>
                </div>
                <div class="modal-body">

                    <form class="form-horizontal" role="form" id="editform">
                        <div class="form-group">
                            <label for="inputTitle" class="col-sm-3 control-label">名称:</label>
                            <div class="col-sm-8">
                                <input type="email" class="form-control" id="inputTitle">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputRemark" class="col-sm-3 control-label">备注:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" id="inputRemark">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">控制器 / 方法:</label>
                            <div class="col-sm-8" >
                                <input type="text" class="form-control" disabled id="func">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">创建时间:</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" disabled id="ctime">
                            </div>
                        </div>
                        
                        <input type="hidden" id="hidden_id">
                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="modalclose">Close</button>
                    <button type="button" class="btn btn-primary" id="submit_btn">Save changes</button>
                </div>
            </div>
        </div>
    </div>
<script>
    function check(){
        if ($("#title").val()=='') {
            alert('名称不能为空');
            return false;
        }
        if ($("#name").val()=='') {
            alert('控制器/方法 不能为空');
            return false;
        }
        return true;
    }

    $("a:contains('编辑')").click(function(){
        var id = $(this).parents('tr').children('td').eq(0).html();
        var title = $(this).parents('tr').children('td').eq(1).attr('title');
        var remark = $(this).parents('tr').children('td').eq(3).html();
        var func = $(this).parents('tr').children('td').eq(2).html();
        var ctime = $(this).parents('tr').children('td').eq(4).html();
        $('#hidden_id').val(id);
        $('#inputTitle').val(title);
        $('#inputRemark').val(remark);
        $('#func').val(func);
        $('#ctime').val(ctime);

        $('#submit_btn').click(function(){
            var id = $('#hidden_id').val();
            var title = $('#inputTitle').val();
            var remark = $('#inputRemark').val();
            $.ajax({
                url:"<{:U('Admin/rule_edit')}>",
                type:'post',
                data:{'id':id,'title':title,'remark':remark},
                success:function(data){
                    if (data == '1') {
                        alert('修改成功');
                        window.location.reload();
                    }else{
                        alert('修改失败');
                        window.location.reload();
                    }
                }
            })
        })
    })


</script>
</block>
